<!--
 * @Descripttion: 编辑文章
 * @Author: xiao li
 * @Date: 2020-07-06 12:17:07
 * @LastEditors: xiao li
 * @LastEditTime: 2021-04-28 14:21:06
-->
<template>
    <div class="website-news">
        <top-nav :title="navTitle" :isBack="true"></top-nav>
        <div class="page-main">
          <el-row class="page-top-operate">
            <lb-button type='primary' icon='el-icon-plus' @click="addEditArticle('add')">添加文章</lb-button>
          </el-row>
          <el-row class="page-search-form">
            <el-form @submit.native.prevent :inline="true" :model="searchForm" ref="searchForm">
              <el-form-item label='标题' prop="keyword">
                 <el-input v-model="searchForm.keyword" placeholder="请输入标题"></el-input>
              </el-form-item>
              <el-form-item>
                <lb-button size="medium" type="primary" icon="el-icon-search" style="margin-right:5px" @click="getTableDataList(1)">{{$t('action.search')}}</lb-button>
                <lb-button size="medium" icon="el-icon-refresh-left" style="margin-right:5px" @click="resetForm('searchForm')">{{$t('action.reset')}}</lb-button>
              </el-form-item>
            </el-form>
          </el-row>
          <el-table
          v-loading='loading'
          :data="tableData"
          :header-cell-style="{background:'#f5f7fa',color:'#606266'}"
          style="width: 100%">
            <el-table-column
              prop="id"
              label="ID">
            </el-table-column>
            <el-table-column
              prop="title"
              label="标题">
            </el-table-column>
            <el-table-column
              label="封面图">
                <template slot-scope="scope">
                  <lb-image  :src="scope.row.cover" />
                </template>
            </el-table-column>
            <!-- <el-table-column
              prop="view_count"
              label="浏览量">
            </el-table-column> -->
            <el-table-column
              prop="top"
              label="排序值">
            </el-table-column>
            <el-table-column
              prop="create_time"
              label="创建时间">
              <template slot-scope="scope">
                <div>{{scope.row.create_time | handleTime(1)}}</div>
                <div>{{scope.row.create_time | handleTime(2)}}</div>
              </template>
            </el-table-column>
            <el-table-column
              prop="status"
              label="是否启用">
              <template slot-scope="scope">
                <el-switch
                  v-model="scope.row.status"
                  :active-value="1"
                  :inactive-value="0"
                  @change="handleSwitchChange(scope.row.status,scope.row.id)"
                  >
                </el-switch>
              </template>
            </el-table-column>
            <el-table-column
              prop="name"
              label="操作">
                  <template slot-scope="scope">
                    <div class="table-operate">
                      <lb-button size='mini' plain type='primary' @click="addEditArticle('edit',scope.row.id)">{{$t('action.edit')}}</lb-button>
                      <lb-button size='mini' plain type='danger' @click="confirmDelArticle(scope.row.id)">{{$t('action.delete')}}</lb-button>
                    </div>
                  </template>
            </el-table-column>
          </el-table>
          <lb-page
            :batch='false'
            :page='searchForm.page'
            :pageSize='searchForm.limit'
            :total='total'
            @handleSizeChange='handleSizeChange'
            @handleCurrentChange='handleCurrentChange'
            >
            </lb-page>
        </div>
    </div>
</template>

<script>
export default {
  data () {
    return {
      navTitle: '',
      searchForm: {
        keyword: '',
        page: 1,
        limit: 10,
        id: ''
      },
      tableData: [],
      total: 0,
      loading: false
    }
  },
  created () {
    let {id, title} = this.$route.query
    this.searchForm.id = id
    this.navTitle = title
  },
  activated () {
    this.getTableDataList()
  },
  components: {
  },
  methods: {
    resetForm (name) {
      this.$refs[name].resetFields()
      this.getTableDataList(1)
    },
    /**
     * @method 添加、编辑文章
     */
    addEditArticle (type, aid) {
      let {id} = this.searchForm
      let url = type === 'add' ? `/website/editNews?wid=${id}` : `/website/editNews?wid=${id}&id=${aid}`
      this.$router.push(url)
    },
    /**
     * @method 获取列表数据
     */
    getTableDataList (page) {
      this.loading = true
      this.searchForm.page = page || this.searchForm.page
      this.$api.getWebsiteContentList(this.searchForm).then(res => {
        console.log(res)
        this.loading = false
        if (res.code === 200) {
          this.tableData = res.data.data
          this.total = res.data.total
        }
      })
    },

    /**
     * @method 文章上下架
     */
    handleSwitchChange (method, wid) {
      let {id} = this.searchForm
      this.$api.updateWebsiteContentInfo({id: wid, method, modular_id: id}).then(res => {
        console.log(res)
        if (res.code === 200) {
          this.$message.success(method === 2 ? this.$t('tips.successDel') : this.$t('tips.successRev'))
          if (method === 2) {
            this.searchForm.page = this.searchForm.page < Math.ceil((this.total - 1) / this.searchForm.limit) ? this.searchForm.page : Math.ceil((this.total - 1) / this.searchForm.limit)
            this.getTableDataList()
          }
        } else {
          if (method === 2) return
          this.getTableDataList()
        }
      })
    },

    /**
     * @method 删除数据
     */
    confirmDelArticle (id) {
      this.$confirm(this.$t('tips.confirmDelete'), this.$t('tips.reminder'), {
        confirmButtonText: this.$t('action.comfirm'),
        cancelButtonText: this.$t('action.cancel'),
        type: 'warning'
      }).then(() => {
        this.handleSwitchChange(2, id)
      })
    },
    handleSizeChange (val) {
      this.searchForm.limit = val
      this.handleCurrentChange(1)
    },
    handleCurrentChange (val) {
      this.searchForm.page = val
      this.getTableDataList()
    }
  },
  filters: {
    handleTime (val, type) {
      let time = type === 1 ? moment(val * 1000).format('YYYY-MM-DD') : type === 2 ? moment(val * 1000).format('HH:mm:ss') : moment(val * 1000).format('YYYY-MM-DD HH:mm:ss')
      return time
    }
  }
}
</script>

<style lang="scss" scoped>
  .website-news{
    .page-main{

    }
  }
</style>
